import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/components/nodata_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import '../../services/address.dart';
import '../../services/dio_manager.dart';
import 'my_calendar_module/my_calendar_room_model.dart';
class CalendarRoomView extends StatefulWidget {
  const CalendarRoomView({Key? key}) : super(key: key);

  @override
  State<CalendarRoomView> createState() => _CalendarRoomViewState();
}

class _CalendarRoomViewState extends State<CalendarRoomView> {


  var dataArr = [];

  @override
  void initState() {
    var timeFormat = DateFormat("yyyy-MM-dd");
    var timeStr = timeFormat.format(DateTime.now());
    // print(timeStr);
    requestDataWithGetCalendarRoom(timeStr);
  }

  requestDataWithGetCalendarRoom(var start_time)async{

    var params = {
      'start_time':'${start_time}',
      'end_time':'${start_time}',
      'allday':'1',
    };
    var json = await DioManager().kkRequest(Address.calendarRoom,params: params,method: 'get');

    MyCalendarRoomModel model = MyCalendarRoomModel.fromJson(json);

    dataArr.clear();

    if(model.data!=null){
      dataArr.addAll(model.data!);
    }
    setState(() {

    });
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseAppBar(child: Text('会议列表'),
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),),
      body: EasyRefresh.custom(

        onRefresh:()async{
          var timeFormat = DateFormat("yyyy-MM-dd");
          var timeStr = timeFormat.format(DateTime.now());
          // print(timeStr);
          requestDataWithGetCalendarRoom(timeStr);
          // requestDataWithGetCalendarRoom();
        } ,

        emptyWidget: dataArr.isEmpty?NoDataPage():null,

        slivers: [
          SliverList(
            delegate: _mySliverChildBuilderDelegate(),
          ),
        ],
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
        MyCalendarRoomItem model = dataArr[index];
        return GestureDetector(
          onTap: () {
            // controller.pushToView(index);
            var params = {
              'name':'${model.name}',
              'id':'${model.id}'
            };
            Get.back(result:params);
          },
          child: Container(
            // margin: EdgeInsets.only(left: 15,right: 15,top: 5),
            color: Colors.white,
            padding: EdgeInsets.all(15),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  child: Text('会议室名称：${model.name}'),
                ),
                Text('最多容纳人数：${model.max}'),
                Text('是否正在使用：${model.isUse==true?'是':'否'}'),
              ],
            ),
          ),
        );
      },
      childCount: dataArr.length,
    );
  }

}
